package com.dayuanit.dy17.atm.task;

import com.dayuanit.dy17.atm.entity.Card;
import com.dayuanit.dy17.atm.entity.Flow;
import com.dayuanit.dy17.atm.entity.TransferOrder;
import com.dayuanit.dy17.atm.enums.FlowTypeEnum;
import com.dayuanit.dy17.atm.enums.TransferOrderStatusEnum;
import com.dayuanit.dy17.atm.exception.BizException;
import com.dayuanit.dy17.atm.mapper.CardMapper;
import com.dayuanit.dy17.atm.mapper.FlowMapper;
import com.dayuanit.dy17.atm.service.ITransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Component
public class TransferInTask extends BaseTask {

    @Autowired
    private ITransferService transferService;

    @Scheduled(cron = "0/15 * * * * ?")
    @Override
    public void doTask() {
        System.out.println("----------TransferInTask--------------");
        processTransferIn();
    }

    private void processTransferIn() {
        List<TransferOrder> transferOrders = transferService.loadDelayTransferOrder(TransferOrderStatusEnum.wait_transfer);
        for (TransferOrder order : transferOrders) {
            try {
                System.out.println(">>>>>>>>处理订单" + order.getId() + "  begin");
                transferService.transferIn(order);
                System.out.println(">>>>>>>>处理订单" + order.getId() + "  end");
            } catch (Exception e) {
                e.printStackTrace();
                // 把订单状态给为待退款
                transferService.changeOrderStatus(order, TransferOrderStatusEnum.rollback_transfer);
            }
        }

    }
}
